package controllers

import (
	"mypassword/utils"
	"os"
	"path/filepath"
)

type KeyPairController struct {
	BaseController
}

func (c *KeyPairController) Generate() {
	// 检查是否已存在密钥对
	if _, err := os.Stat(filepath.Join("cert", "private.pem")); err == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "密钥对已存在",
		}
		c.ServeJSON()
		return
	}

	// 生成新的密钥对
	rsaUtil := utils.NewRSAUtil()
	if err := rsaUtil.GenerateKeyPair(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": err.Error(),
		}
	} else {
		c.Data["json"] = map[string]interface{}{
			"success": true,
			"message": "密钥对生成成功",
		}
	}
	c.ServeJSON()
}
